package org.top10.parser;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.util.NodeList;
import org.top10.entity.CollectEntity;
import org.top10.entity.Emotion;
import org.top10.entity.Finance;
import org.top10.entity.News;
import org.top10.entity.Reading;
import org.top10.executor.ParseSource;
import org.top10.util.TOP10Global;
import org.w3c.dom.Node;

public class MsnTask extends Task{
	private static final int webSiteID = TOP10Global.WEB_SITE_ID_MSM;// 对应网站ID
	private static final String ENCoding = TOP10Global.Encoding_GDK;// 对应网站ID
	
	public MsnTask() {
		super(webSiteID, ENCoding);
	}

	public void loadParser() {
		PARSER.add(new MsnReadingParser());
		PARSER.add(new MsnFinanceParser());
		PARSER.add(new MsnEmotionParser());
	}
	public static void main(String arg[]) throws Exception{
		new MsnEmotionParser().dealText(ENCoding);
		
	}
	
}
abstract class MSNParser extends TOPParserImpl {
	public MSNParser(String title,int type,String url) {
		super(title,type, url);
	}

	public abstract List parse(ParseSource doc) throws Exception;
	public abstract CollectEntity build(Node node, int rank) throws Exception;
	
}

class MsnReadingParser extends MSNParser{
	private static final String SIGN="最新章节";
	private static final String Base_URL="http://msn.qidian.com/";
	public MsnReadingParser() {
		super("读书排行", TOP10Global.TYPE_Reading, "http://msn.qidian.com/BookBoard.aspx?order=7");
		// TODO Auto-generated constructor stub
	}

	public Reading build(Node a, int rank) throws Exception {
		String url = getAttrValueByTag(a, "href");
		if(url.indexOf("ShowBook.aspx?")==-1)return null;
		Reading v = new Reading();
		v.setRank(rank);
		v.setLinkUrl(Base_URL+""+url);
		v.setName(a.getTextContent());
		return v;
	}
	
	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByText(body,"td",SIGN).getParentNode().getParentNode();
		if(node!=null){
			ArrayList<Node> as = new ArrayList<Node>();
			ArrayList<CollectEntity> entitys = new ArrayList<CollectEntity>();
			getAllTagsByParent(node,"a",as);
			int i=1;
			for (Node a : as){
				CollectEntity v = build(a,i);
				if(v!=null){
					System.out.println(v.toString());
					entitys.add(v);
					i++;
					if(i>10)break;
				}
			}
			return entitys;
		}
		
		return null;
	}
	
}
class MsnFinanceParser extends MSNParser{
	private static final String SIGN="24小时图文排行榜";
	public MsnFinanceParser() {
		super("财经排行", TOP10Global.TYPE_Finance, "http://business.msn.com.cn/MsnFinance/index.shtml");
	}

	public Finance build(Node a, int rank) throws Exception {
		String url = getAttrValueByTag(a, "href");
		Finance v = new Finance();
		v.setRank(rank);
		v.setLinkUrl(url);
		v.setName(a.getTextContent());
		return v;
	}
	
	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByText(body,"span",SIGN).getParentNode().getParentNode();
		if(node!=null){
			ArrayList<Node> as = new ArrayList<Node>();
			ArrayList<CollectEntity> entitys = new ArrayList<CollectEntity>();
			getAllTagsByParent(node,"a",as);
			int i=1;
			for (Node a : as){
				CollectEntity v = build(a,i);
				if(v!=null){
					System.out.println(v.toString());
					entitys.add(v);
					i++;
					if(i>10)break;
				}
			}
			return entitys;
		}
		return null;
	}
}
class MsnEmotionParser extends MSNParser{
	private static final String SIGN="abox1";
	private static final String BASE_URL="http://msn.yoka.com";
	public MsnEmotionParser() {
		super("情感排行", TOP10Global.TYPE_Emotion,BASE_URL );
	}

	public Emotion build(Node a, int rank) throws Exception {
		String url = getAttrValueByTag(a, "href");
		if(url.indexOf("/psycho/")==-1)return null;
		Emotion v = new Emotion();
		v.setRank(rank);
		v.setLinkUrl(BASE_URL+""+url);
		v.setName(a.getTextContent());
		return v;
	}
	
	public List parse(ParseSource doc) throws Exception {
		Node body = doc.getDocSource().getElementsByTagName("BODY").item(0);
		Node node = getNodeByAttr("div","id",SIGN,body);
		if(node!=null){
			ArrayList<Node> as = new ArrayList<Node>();
			ArrayList<CollectEntity> entitys = new ArrayList<CollectEntity>();
			getAllTagsByParent(node,"a",as);
			int i=1;
			for (Node a : as){
				CollectEntity v = build(a,i);
				if(v!=null){
					System.out.println(v.toString());
					entitys.add(v);
					i++;
					if(i>10)break;
				}
			}
			return entitys;
		}
		return null;
	}
}